package com.chenjt.netty.ten_zerocopy;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.Socket;

/**
 * 客户端读取磁盘上的文件，将文件通过socket的outputStream发送到服务器端，记录发送完成的时间
 *
 * @author chenjt
 * @date 2024/9/2 10:19
 */
public class OldClient {

    public static void main(String[] args) throws Exception{
        Socket socket = new Socket("localhost", 8899);
        // 指定磁盘上的文件（几百兆）
        String fileName = "";
        FileInputStream inputStream = new FileInputStream(fileName);
        // 读取数据之后写到网络的另一端
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        byte[] buffer = new byte[4096];
        long readCount;
        long total = 0;

        long startTime = System.currentTimeMillis();

        while((readCount = inputStream.read(buffer)) >= 0){
            total += readCount;
            dataOutputStream.write(buffer);
        }

        System.out.println("发送总字节数：" + total + "，耗时：" + (System.currentTimeMillis() - startTime));

        dataOutputStream.close();
        socket.close();
        inputStream.close();

    }

}
